package com.framework.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.BannerDao;
import com.framework.entity.or.OrAppMenu;
import com.framework.entity.or.OrBanner;
import com.framework.entity.or.OrBannerType;
import com.framework.exception.RRException;
import com.framework.page.PageUtils;
import com.framework.page.Query;
import com.framework.service.AppMenuService;
import com.framework.service.BannerService;
import com.framework.service.app.BannerTypeService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wq
 *
 */
@Transactional(rollbackFor = Exception.class)
@Service("bannerService")
public class BannerServiceImpl extends ServiceImpl<BannerDao, OrBanner> implements BannerService {


    @Autowired
    private AppMenuService appMenuService;

    @Autowired
    private BannerTypeService bannerTypeService;


    @Override
    public List<OrBanner> queryBannerList(Integer type, Long deptId) {
        if (type == 0 || type == null) {
            throw new RRException("9000", "类型不能为空");
        }
        if (deptId == 0 || deptId == null) {
            throw new RRException("9000", "所属渠道不能为空");
        }
        OrBanner banner = new OrBanner();
        banner.setType(type);
        banner.setDeptId(deptId);
        banner.setState(1);
        List<OrBanner> list = baseMapper.queryBannerList(banner);
        return list;
    }

    @Override
    public Map<String, Object> queryBannerAndMen(Long deptId) {
        Map<String, Object> map = new HashMap<>();
        if (deptId == null) {
            throw new RRException("9000", "所属渠道不能为空");
        }
        List<OrBanner> headBannerList = queryBannerList(1, deptId);
        //List<OrBanner> bottomBannerList = queryBannerList(2, deptId);
        //List<OrBanner> activityBannerList = queryBannerList(3, deptId);
        //首页头部banner图
        map.put("headBannerList", headBannerList);
        //首页底部banner图
        map.put("bottomBannerList", new ArrayList<>());
        //活动底部banner图
        map.put("activityBannerList", new ArrayList<>());
        List<OrAppMenu> homeMenuList = appMenuService.queryAppMenuList(1, deptId);
        //首页app菜单按钮
        map.put("indexMenuList", homeMenuList);
        return map;
    }

    @Override
    public OrBanner queryBannerInfoById(Long id) {
        OrBanner b = new OrBanner();
        b.setBannerId(id);
        return baseMapper.selectOne(b);
    }

    @Override
    public PageUtils queryBannerListForAdmin(Map<String, Object> params) {
        Long deptId = (Long) params.get("deptId");
        List<OrBannerType> bannerTypes = bannerTypeService.queryListByParentType(1);

        List<Integer> typeList = bannerTypes.stream().map(OrBannerType::getType).collect(Collectors.toList());

        Integer type = 0;
        if (params.get("type") != null) {
            type = Integer.valueOf(params.get("type").toString());
        }


        Integer state = 0;
        if (params.get("state") != null) {
            state = Integer.valueOf(params.get("state").toString());
        }
        Page<OrBanner> page = this.selectPage(new Query<OrBanner>(params).getPage(), new EntityWrapper<OrBanner>()
                .eq("dept_id", deptId)
                .eq(state != 0, "state", state)
                .in(type == 0, "type", typeList)
                .eq(type != 0, "type", type)
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryLessonPageByBanner(Map<String, Object> params) {
        Long deptId = (Long) params.get("deptId");
        List<OrBannerType> bannerTypes = bannerTypeService.queryListByParentType(2);

        List<Integer> typeList = bannerTypes.stream().map(OrBannerType::getType).collect(Collectors.toList());

        Integer type = 0;
        if (params.get("type") != null) {
            type = Integer.valueOf(params.get("type").toString());
        }


        Integer state = 0;
        if (params.get("state") != null) {
            state = Integer.valueOf(params.get("state").toString());
        }
        Page<OrBanner> page = this.selectPage(new Query<OrBanner>(params).getPage(), new EntityWrapper<OrBanner>()
                .eq("dept_id", deptId)
                .eq(state != 0, "state", state)
                .in(type == 0, "type", typeList)
                .eq(type != 0, "type", type)
        );
        return new PageUtils(page);


    }

    @Override
    public List<OrBanner> queryBannerListLimit(Integer limit, Integer type, Long deptId) {

        List<OrBanner> orBanners = baseMapper.selectPage(new RowBounds(0, limit), new EntityWrapper<OrBanner>()
                .eq("type", type)
                .eq("dept_id", deptId)
                .eq("state", 1)
                .orderBy("create_date", false));

        return orBanners;
    }


}
